/*
 * Title: WADS
 * Description: Web-based Auto flow Documents System
 * Copyright: 2007
 * Company: CoE
 * @author: Mr.Nattasan Wang-Aphisit
 * @version 1.1
 */

package wads.app;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import wads.drawing.node.CaseNode;

public class GridSettingsDialog extends JDialog{
//  private static final long serialVersionUID = 546349961680965535L;
  
  private GridPreviewPanel previewPanel;
  private JSlider gridSizeSlider;
  private int gridSize;

  public GridSettingsDialog(int gridSize) {
    super();

    //Init panel
    previewPanel = new GridPreviewPanel(gridSize);

    // Set up the UI
    JLabel title = new JLabel("Grid Size Settings");
    title.setAlignmentX(Component.LEFT_ALIGNMENT);

    gridSizeSlider = new JSlider(10, 100, gridSize);
    gridSizeSlider.addChangeListener(new GridSettingsListener());

    //Add buttons
    JPanel buttonsPanel = new JPanel();
    JButton okButton = new JButton("Ok");
    JButton cancelButton = new JButton("Cancel");
    okButton.addActionListener(new ActionListener(){
      @Override
      public void actionPerformed(ActionEvent evt){
        setGridSize(gridSizeSlider.getValue());
        GridSettingsDialog.this.setVisible(false);
        }
      });
    
    cancelButton.addActionListener(new ActionListener(){
      @Override
      public void actionPerformed(ActionEvent evt){
        GridSettingsDialog.this.setVisible(false);
        }
      });
    
    buttonsPanel.add(okButton);
    buttonsPanel.add(cancelButton);

    JPanel bottomPanel = new JPanel();
    bottomPanel.setLayout(new BorderLayout());
    bottomPanel.add(gridSizeSlider, "North");
    bottomPanel.add(buttonsPanel, "South");

    previewPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
    bottomPanel.setAlignmentX(Component.LEFT_ALIGNMENT);

    JPanel overallPanel = new JPanel();
    overallPanel.setLayout(new BorderLayout());

    overallPanel.add(title,"North");
    overallPanel.add(previewPanel,"Center");
    overallPanel.add(bottomPanel,"South");
    overallPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

    getContentPane().add(overallPanel);

    setSize(400,300);
    setResizable(true);
    setModal(true);
  }// end of GridSettingDialog<Constructure Method>

  public void setGridSize(int gridSize) { this.gridSize = gridSize; }
  public int getGridSize() { return gridSize; }

  private class GridSettingsListener implements ChangeListener {
    @Override
    public void stateChanged(ChangeEvent e) {
      previewPanel.setGridSize(((JSlider) e.getSource()).getValue());
      previewPanel.repaint();
    }
  }
  
  private class GridPreviewPanel extends JPanel {
//    private static final long serialVersionUID = 2929421224142106026L;
    
    private int gridSize;

    public GridPreviewPanel() {
      gridSize = CaseNode.DEFAULT_NODE_ICON_WIDTH;
      setBackground(Color.white);
    }

    public GridPreviewPanel(int gridSize) {
      this.gridSize = gridSize;
      setBackground(Color.white);
    }

    public void setGridSize(int gridSize) { this.gridSize = gridSize; }
    public int getGridSize() { return gridSize; }

    public void clearScreen(Graphics g) {
      //Clear panel
      super.paintComponent(g);
    }

    @Override
    public void paintComponent(Graphics g) {
      clearScreen(g);
      drawGrid(g);
    }

    public void drawGrid() { drawGrid(getGraphics()); }
    
    public void drawGrid(Graphics g) {
      Graphics2D g2d = (Graphics2D) g;
      Color      originalColor = g2d.getColor();
      g2d.setColor(Color.lightGray);

      // draw horizontal grid lines
      for (int y = 0; y < getSize().height; y+=getGridSize())
        g2d.drawLine(0,y,getSize().width,y);
      
      // draw vertical grid lines
      for (int x = 0; x < getSize().width; x+=getGridSize())
        g2d.drawLine(x,0,x,getSize().height);
      
      g2d.setColor(originalColor);
    }//end of drawGrid(Graphics)
  }//end of Inner-Class GridPreviewPanel
}

